using Cysharp.Threading.Tasks;
using UnityEngine;

namespace M13.Game.Entity.Player
{
    public class PlayerActionEatup : PlayerStateBase
    {
        private bool _canExit;

        public PlayerActionEatup(string stateName)
        {
            this.StateName =stateName;
        }
        public override void Enter()
        {
            animator.Play("Eat", 0, 0f);
            EatUp();
            _canExit = false;
        }

        private async void EatUp()
        {
            AnimPlayTask = UniTask.WaitUntil(() => GetAnimPlayProgress() > 0.99f && IsCurrentAnimPlaying("Eat"));
            await UniTask.WhenAll(AnimPlayTask);
            _canExit = true;
            stateMachine.SwitchOnState();
        }

        public override bool TryEnter()
        {
            return !stateMachine.IsCurrentState(GetType());
        }

        public override string GetAnimName()
        {
            return "Eat";
        }

        public override UniTask GetAnimPlayTask()
        {
            AnimPlayTask = UniTask.WaitUntil(() => GetAnimPlayProgress() > 0.99f && IsCurrentAnimPlaying(GetAnimName()));
            return AnimPlayTask;
        }


        public override bool TryExit()
        {
            return _canExit;
        }
    }
}